package appVeterinaria.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import appVeterinaria.excepcion.DAOExcepcion;
import appVeterinaria.modelo.Doctor;
import appVeterinaria.util.ConexionBD;

public class DoctoresDAO extends BaseDAO {

	public Collection<Doctor> buscarPorNombreDoctor(String nombres)
			throws DAOExcepcion {
		String query = "select idDoctor,nombres,apellidos,email,telefono,idusuario,password,administrador from doctor where nombres like ?";
		Collection<Doctor> lista = new ArrayList<Doctor>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, "%" + nombres + "%");
			rs = stmt.executeQuery();
			while (rs.next()) {
				Doctor vo = new Doctor();
				vo.setIdPersona(rs.getInt("idDoctor"));
				vo.setNombres(rs.getString("nombres"));
				vo.setApellidos(rs.getString("apellidos"));
				vo.setEmail(rs.getString("email"));
				vo.setTlfMovil(rs.getInt("telefono"));
				vo.setUsuario(rs.getString("idusuario"));
				vo.setPassword(rs.getString("password"));				
				vo.setAdmin(rs.getBoolean("administrador"));
				lista.add(vo);
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		//System.out.println(lista.size());
		return lista;
	}
	
	public Collection<Doctor> buscarPorCodigo(int idDoctor)
			throws DAOExcepcion {
		String query = "select idDoctor, nombres,apellidos,email,telefono,idusuario,password,administrador from Doctor where idDoctor=?";
		Collection<Doctor> lista = new ArrayList<Doctor>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, idDoctor);
			rs = stmt.executeQuery();
			while (rs.next()) {
				Doctor vo = new Doctor();
				vo.setIdPersona(rs.getInt("idDoctor"));
				vo.setNombres(rs.getString("nombres"));
				vo.setApellidos(rs.getString("apellidos"));
				vo.setTlfMovil(rs.getInt("telefono"));
				vo.setUsuario(rs.getString("idusuario"));
				vo.setPassword(rs.getString("password"));				
				vo.setAdmin(rs.getBoolean("administrador"));
				lista.add(vo);
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		//System.out.println(lista.size());
		return lista;
	}

	public Doctor insertar(Doctor vo) throws DAOExcepcion {
		String query = "insert into doctor(nombres,apellidos,email,telefono, idUsuario,password, administrador) values (?,?,?,?,?,?,?)";
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);			
			stmt.setString(1, vo.getNombres());
			stmt.setString(2, vo.getApellidos());
			stmt.setString(3, vo.getEmail());
			stmt.setInt(4, vo.getTlfMovil());
			stmt.setString(5, vo.getUsuario());
			stmt.setString(6, vo.getPassword());
			stmt.setBoolean(7, vo.isAdmin());
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo insertar");
			}
			// Obtener el ultimo id
			int id = 0;
			query = "select last_insert_id()";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			if (rs.next()) {
				id = rs.getInt(1);
			}
			vo.setIdPersona(id);

		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}

	public Doctor obtener(int idDoctor) throws DAOExcepcion {
		Doctor vo = new Doctor();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			String query = "select idDoctor, nombres,apellidos,email,telefono,idusuario,password,administrador from Doctor where idDoctor=?";
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, idDoctor);
			rs = stmt.executeQuery();
			if (rs.next()) {
				vo.setIdPersona(rs.getInt(1));
				vo.setNombres(rs.getString(2));
				vo.setApellidos(rs.getString(3));
				vo.setEmail(rs.getString(4));
				vo.setTlfMovil(rs.getInt(5));
				vo.setUsuario(rs.getString(6));
				vo.setPassword(rs.getString(7));
				vo.setAdmin(rs.getBoolean(8));
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}

	public void eliminar(int idDoctor) throws DAOExcepcion {
		String query = "delete from Doctor WHERE idDoctor=?";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, idDoctor);
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo eliminar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
	}

	public Doctor actualizar(Doctor vo) throws DAOExcepcion {
		String query = "update Doctor set nombres=?,apellidos=?,email=?,telefono=?,idusuario=?,password=?,administrador=? where iddoctor=?";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, vo.getNombres());
			stmt.setString(2, vo.getApellidos());
			stmt.setString(3,vo.getEmail());
			stmt.setInt(4, vo.getTlfMovil());
			stmt.setString(5, vo.getUsuario());
			stmt.setString(6, vo.getPassword());
			stmt.setBoolean(7, vo.isAdmin());
			stmt.setInt(8, vo.getIdPersona());
			
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo actualizar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}

	public Collection<Doctor> listar() throws DAOExcepcion {
		Collection<Doctor> c = new ArrayList<Doctor>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			String query = "select idDoctor, nombres,apellidos,email,telefono,idusuario,password,administrador from Doctor order by nombres";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			while (rs.next()) {
				Doctor vo = new Doctor();
				vo.setIdPersona(rs.getInt("idDoctor"));
				vo.setNombres(rs.getString("nombres"));
				vo.setApellidos(rs.getString("apellidos"));
				vo.setEmail(rs.getString("email"));
				vo.setTlfMovil(rs.getInt("telefono"));
				vo.setUsuario(rs.getString("idusuario"));
				vo.setPassword(rs.getString("password"));				
				vo.setAdmin(rs.getBoolean("administrador"));
				c.add(vo);
			}

		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return c;
	}

	public int contar(String idUsuario) throws DAOExcepcion {
		int total =0;
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			String query = "select count(idusuario) from veterinaria_db.doctor where idusuario=?";
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, idUsuario);
			rs = stmt.executeQuery();
			if (rs.next()) {
				total = rs.getInt(1);
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return total;
	}

	public Doctor validarUsuario(String idUsuario, String password)
			throws DAOExcepcion{
		String query = "select idDoctor, nombres, apellidos,"
				+ "email, telefono,"
				+ "administrador from doctor where idusuario=? and password=?";
		
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		Doctor vo = new Doctor();
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, idUsuario);
			stmt.setString(2, password);
			rs = stmt.executeQuery();

			if (rs.next()) {
				vo.setIdPersona(rs.getInt("idDoctor"));
				vo.setNombres(rs.getString("nombres"));
				vo.setApellidos(rs.getString("apellidos"));
				vo.setEmail(rs.getString("email"));
				vo.setTlfMovil(rs.getInt("telefono"));
				vo.setAdmin(rs.getBoolean("administrador"));
			} else {
				throw new DAOExcepcion("No existe");
			}
		} catch (DAOExcepcion e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}
	
	public Collection<Doctor> listarDoctorPorAdmin(boolean admin) throws DAOExcepcion {
		Collection<Doctor> d = new ArrayList<Doctor>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			String query = "select idDoctor, nombres,apellidos,email,telefono,idusuario,password,administrador from Doctor where administrador=? order by nombres";
			stmt = con.prepareStatement(query);
			stmt.setBoolean(1, admin);
			rs = stmt.executeQuery();
			while (rs.next()) {
				
				Doctor vo = new Doctor();
				vo.setIdPersona(rs.getInt("idDoctor"));
				vo.setNombres(rs.getString("nombres"));
				vo.setApellidos(rs.getString("apellidos"));
				vo.setEmail(rs.getString("email"));
				vo.setTlfMovil(rs.getInt("telefono"));
				vo.setUsuario(rs.getString("idusuario"));
				vo.setPassword(rs.getString("password"));				
				vo.setAdmin(rs.getBoolean("administrador"));
				d.add(vo);
			}

		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return d;
	}	
	
	public Collection<Doctor> buscarPorApellidoDoctor(String nombres)
			throws DAOExcepcion {
		String query = "SELECT idDoctor, nombres,apellidos,email,telefono,idusuario,password,administrador from doctor where apellidos=?";
		Collection<Doctor> lista = new ArrayList<Doctor>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, "%" + nombres + "%");
			rs = stmt.executeQuery();
			while (rs.next()) {
				Doctor vo = new Doctor();
				vo.setIdPersona(rs.getInt("idDoctor"));
				vo.setNombres(rs.getString("nombres"));
				vo.setApellidos(rs.getString("apellidos"));
				vo.setTlfMovil(rs.getInt("telefono"));
				vo.setUsuario(rs.getString("idusuario"));
				vo.setPassword(rs.getString("password"));				
				vo.setAdmin(rs.getBoolean("administrador"));
				lista.add(vo);
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		//System.out.println(lista.size());
		return lista;
	}
	
}

